<template>
  <div class="about">
    <div class="wrap">
      <ul class="nav">
        <li
          v-for="(item, index) in fenlei"
          :key="item._id"
          @click="change(index, item._id)"
          :class="{ active: index == flag }"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>

    <ul class="con">
      <shop
        v-for="item in result"
        :key="item._id"
        :item="item"
        @click.native="gotodetail(item)"
      />
      <!-- 把每个商品封装子组件，点击的时候因为是子组件 所以要加修饰符native -->
    </ul>
  </div>
</template>
<script>
import shop from "../components/Shop.vue";
export default {
  data() {
    return {
      fenlei: [],
      list: [],
      flag: 0,
      result: [],
    };
  },
  components: {
    shop,
  },
  mounted() {
    this.getlenlei();
    this.getlist();
  },
  methods: {
    getlenlei() {
      this.$axios.get("fenlei.json").then((res) => {
        console.log(res);
        this.fenlei = res.data.result;
      });
    },
    //获取分类数据
    getlist() {
      this.$axios.get("list.json").then((res) => {
        this.list = res.data.result;
        this.change(0, "59f1e1ada1da8b15d42234e9");
      });
    },
    change(index, cid) {
      this.flag = index;
      this.result = this.list.filter((item) => {
        return item.cid == cid;
      });
    },
    gotodetail(item) {
      this.$router.push({ path: "/detail", query: { item: item } });
    },
  },
};
</script>
<style lang="scss">
.wrap {
  width: 100%;
  overflow: scroll;
  .nav {
    display: flex;
    li {
      width: 80px;
      flex-shrink: 0;
      text-align: center;
      line-height: 50px;
      height: 50px;
    }
    .active {
      color: brown;
      border-bottom: 2px solid brown;
    }
  }
}

.con {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  li {
    width: 45%;
    border: 1px solid #ccc;
    img {
      width: 100%;
    }
  }
}
</style>
